<?php

/**
 * h5.php
 * h5支付/公众号支付获取支付数据
 * Author: netman
 * DateTime:  2023/10/20 4:37
 */
class Service_Api_Payment_H5 extends Contract_Api_Payment
{
    use Trait_Api_Common;
    use Trait_Api_Payment;

    /**
     * 获取支付数据
     * @param $params
     * @return array
     * @throws Exception
     * User: netman
     * Date: 2023/10/20 15:16
     */
    function get_pay_data($params): array
    {
        $order_sn = $params['ordersn'];
        $is_wx_client = $params['is_wx_client'];
        $comefrom =  $params['comefrom'];//h5,wxxcx

        // 验证订单
        $check_order = Model_Api_V2_Payment::ordersn_checked($order_sn);

        if (!$check_order['status']) {
            if ($check_order['data']['sign'] == 24) {
                return array(
                    'is_payed' => 1,
                    'id' => $check_order['data']['id'],
                );
            } else {
                throw new Exception($check_order['msg']);
            }
        }

        $info = Model_Member_Order::order_info($order_sn);
        $info['series_number'] = St_Product::product_series($info['productautoid'], $info['typeid']);
        //"0元"订单,免费产品
        if ($info['pay_price'] == 0) {
            $zero_res = $this->zero_pay($order_sn);
            if ($zero_res['status']) {
                return array(
                    'is_payed' => 1,
                    'id' => $info['id'],
                );
            } else {
                throw new Exception($zero_res['msg']);
            }
        }

        //检查是否有附加产品
        $additional = DB::select()->from('member_order')->where('pid', '=', $info['id'])->execute()->as_array();

        //单产品支付价格,在没有子产品的情况下等于订单总支付价格
        $info['single_pay_price'] = $info['pay_price'];
        if ($additional) {
            foreach ($additional as &$sub) {
                $sub = Model_Member_Order::order_info($sub['ordersn']);
                $info['single_pay_price'] = $info['single_pay_price'] - $sub['pay_price'];
            }
        }

        //支付方式
        $pay_method = $this->pay_method($is_wx_client,$comefrom);
        foreach ($pay_method['online'] as $k=>$v) {
            if($info['typeid']==196 && $v['pinyin']=='wallet'){
                unset($pay_method['online'][$k]);
            }
        }

        return array(
            'is_payed'=> 0,
            'info' => $info,
            'pay_method' => $pay_method,
            'additional' => $additional,
            'btn_title' => $info['status'] == 0 ? '查看订单' : '立即支付'
        );
    }

    /**
     * 支付方式
     * @return mixed
     */
    private function pay_method($is_wx_client = false,$comefrom='')
    {
        $online = array();
        $offline = array();
        $order = array();
        //微信客户端

        $order = array();

        //支持的支付方式
        $pay_method = $this->get_pay_method();
        foreach ($pay_method as $way) {

            if($comefrom=='wxxcx' && !in_array($way['pinyin'] ,['wxpay','icbcpay','wallet'])){
                continue;
            }
            if($comefrom=='h5'){
                if ($way['pinyin'] == 'allinpay') {
                    continue;
                }

                //微信支付只能在微信浏览器里面才能使用
                if ($is_wx_client == 0 && $way['pinyin'] == 'wxpay') {
                    continue;
                }

                //微信h5支付不能在微信浏览器里操作
                if ($is_wx_client && $way['pinyin'] == 'wxh5') {
                    continue;
                }
            }

            //判断是否是线下支付
            if ($way['id'] != 6) {
                $online[] = $way;
                $order[] = isset($way['displayorder']) ? $way['displayorder'] : 0;
            } else {
                $offline["6"] = $way;
            }
        }

        if ($online) {
            $rs['online'] = $online;
            $rs['online'][key($rs['online'])]['selected'] = true;
        }

        $rs['offline'] = $offline;
        //默认选择第一个

        return $rs;
    }

    /**
     * @function 扩展支付方式,第三方支付应用.
     * @return mixed
     */
    private function get_pay_method()
    {
        $arr = DB::select()
            ->from('payset')
            ->where('isopen', '=', 1)
            ->and_where_open()
            ->and_where('platform', '=', 0)
            ->or_where('platform', '=', 2)
            ->and_where_close()
            ->order_by('displayorder', 'ASC')
            ->execute()
            ->as_array();
        foreach ($arr as &$row) {
            if ($row['issystem'] != 1) {
                //微信JSAPI支付(对应目录/plugins/wxpay/mobile/uniapp_index)
                if ($row['id'] == 8) {
                    // uniapp重写微信公众支付地址
                    $row['payurl'] = $GLOBALS['cfg_basehost'].'/'.$row['pinyin'].'/mobile/uniapp_index';
                } else {
                    $row['payurl'] = $GLOBALS['cfg_basehost'].'/'.$row['pinyin'].'/mobile';
                }
                if ($row['pinyin'] == 'alipay') {
                    // uniapp重写支付宝支付地址
                    $row['payurl'] = $GLOBALS['cfg_basehost'].'/'.$row['pinyin'].'/mobile/uniapp_show';
                }
                if ($row['pinyin'] == 'icbcpay') {
                    // uniapp重写工行支付地址
                    $row['payurl'] = $GLOBALS['cfg_basehost'].'/'.$row['pinyin'].'/mobile/index';
                }
            } else {
                $row['payurl'] = '';
            }
        }
        return $arr;
    }


}